package me.ereach.multi;

import java.util.ArrayList;
import java.util.List;

public class Main04RaceCondition2 {
    public static void main(String[] args) {
        List<Thread> threads = new ArrayList<>();
        List<DownloadStatus> statuses = new ArrayList<>();

        for (var i = 0; i < 5; i++) {
            var task = new DownloadFileTask();
            var thread = new Thread(task);
            threads.add(thread);
            statuses.add(task.getStatus());
            thread.start();
        }

        for (var thread : threads) {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        int total = 0;

        for (var status : statuses) {
            total += status.getTotalBytes();
        }

        System.out.println("Download bytes: " + total);

        var b = statuses.stream()
                .map(t -> t.getTotalBytes())
                .reduce(0, Integer::sum);
        System.out.println(b);
    }
}
